package hongcaosp.app.android.user.dynamic;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.common.TestConstants;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.DynamicVideo;
import hongcaosp.app.android.modle.bean.IDynamic;
import hongcaosp.app.android.modle.bean.IUser;
import hongcaosp.app.android.modle.bean.IVideo;
import hongcaosp.app.android.modle.bean.Pagebean;
import hongcaosp.app.android.modle.bean.Video;
import hongcaosp.app.android.modle.impl.UserFansModleImpl;
import hongcaosp.app.android.modle.impl.UserModleImpl;
import hongcaosp.app.android.modle.mi.UserFansModle;
import hongcaosp.app.android.modle.mi.UserModle;
import hongcaosp.app.android.user.TotalCallBack;
import xlj.lib.android.base.component.BasePresenter;

public class DynamicPresenter extends BasePresenter {

    private IListView<IDynamic> listView;
    private UserModle userModle;
    private UserFansModle userFansModle;
    private long userId;
    private boolean isUsers;
    private int number;
    private int total;

    public DynamicPresenter(IListView<IDynamic> listView, long userId) {
        this.listView = listView;
        this.userId = userId;
        isUsers = userId > 0;
        userModle = new UserModleImpl();
        userFansModle = new UserFansModleImpl();
    }

    public void freshData(final TotalCallBack totalCallBack) {
        number = 1;
        DataCallBack<Pagebean<DynamicVideo>> dataCallBack = new DataCallBack<Pagebean<DynamicVideo>>() {
            @Override
            public void onGetData(Pagebean<DynamicVideo> data) {
                super.onGetData(data);
                boolean loadMore = false;
                total = data.getList() == null ? 0 : data.getList().size();
                if (data.getTotal() > total) {
                    loadMore = true;
                }
                List<DynamicVideo> videos = data.getList();
                List<IDynamic> videoList = new ArrayList<>();
                videoList.addAll(videos);
                listView.freshList(videoList, loadMore);
                if (totalCallBack != null) {
                    totalCallBack.onTotalSend(data.getTotal());
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                listView.freshList(null, false);
                if (totalCallBack != null) {
                    totalCallBack.onTotalSend(0);
                }
            }
        };
        if (isUsers) {
            userModle.userDynamic((int) userId,UserToken.getDefault().getToken(), 20, number, dataCallBack);
        } else {
            userFansModle.fansDynamic(UserToken.getDefault().getToken(), 20, number, dataCallBack);
        }
    }

    public void loadMore() {
        number++;
        DataCallBack<Pagebean<DynamicVideo>> dataCallBack = new DataCallBack<Pagebean<DynamicVideo>>() {
            @Override
            public void onGetData(Pagebean<DynamicVideo> data) {
                super.onGetData(data);
                boolean loadMore = false;
                total += data.getList() == null ? 0 : data.getList().size();
                if (data.getTotal() > total) {
                    loadMore = true;
                }
                List<DynamicVideo> videos = data.getList();
                List<IDynamic> videoList = new ArrayList<>();
                videoList.addAll(videos);
                listView.addList(videoList, loadMore);

            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                listView.addList(null, false);
            }
        };
        if (isUsers) {
            userModle.userDynamic((int) userId,UserToken.getDefault().getToken(), 20, number, dataCallBack);
        } else {
            userFansModle.fansDynamic(UserToken.getDefault().getToken(), 20, number, dataCallBack);
        }
    }
}
